//
//  SinaPictureView.swift
//  mySina
//
//  Created by 刘莹 on 2016/11/18.
//  Copyright © 2016年 liuying. All rights reserved.
//

import UIKit
private let cellId = "SinaPictureCell"
class SinaPictureView: UICollectionView {

    var pictureInfos: [SinaPictureInfo]? {
        didSet {
            
            
            //外界给配图视图设置数据的时候就会执行
            self.reloadData()
        }
    }
    
    override func awakeFromNib() {
        //注册cell
        let pictureCellNib = UINib(nibName: "SinaPictureCell", bundle: nil)
        self.register(pictureCellNib, forCellWithReuseIdentifier: cellId)
        
        //显示数据 遵守dataSource协议 实现数据方法
        self.dataSource = self
    }
}
    extension SinaPictureView: UICollectionViewDataSource {
        //实现数据源方法
        func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return pictureInfos?.count ?? 0
        }
        //为SinaPictureCell赋值并返回
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! SinaPictureCell
            let pictureInf = self.pictureInfos![indexPath.item]
            cell.pictureInfo = pictureInf
            return cell        }
    }


